<?php


namespace App\Models\CacheModel\live;


use App\Models\Api\BaseCache;
use think\Log;
use AstroTool\Cache\CacheManage;

class LiveDisableSendMsg extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.live_disable_send_msg';

    protected $cacheModel = 'live.disable_send_msg';

    /**
     * 直播间禁言
     * @param $liveId
     * @param $liveRecordId
     * @param $uid
     * @param $data
     * @return bool
     */
    public function userDisableSendMsg($liveId, $liveRecordId, $uid, $data)
    {
        try
        {
            $this->insert($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $liveId . ':' . $liveRecordId . ':' . $uid)->setCache(1);

            return true;
        }
        catch (\Exception $e)
        {
            Log::write("直播间禁言失败：{$e->getMessage()};File:{$e->getFile()};Line:{$e->getLine()}", 'error', true);
            return false;
        }
    }

    /**
     * 获取用户是否被禁言 true 被禁言
     * @param $liveId
     * @param $liveRecordId
     * @param $uid
     * @return bool
     * @throws \Exception
     */
    public function getUserIsDisableSendMsg($liveId, $liveRecordId, $uid)
    {
        $res = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $liveId . ':' . $liveRecordId . ':' . $uid)
            ->getDataAndSet(function () use ($liveId, $liveRecordId, $uid)
            {
                try
                {
                    $num = $this->where(['live_id' => $liveId, 'live_record_id' => $liveRecordId, 'uid' => $uid, 'status' => 1])->count();
                    return $num > 0 ? 1 : 0;
                }
                catch (\Exception $e)
                {
                    return 0;
                }
            });

        return $res > 0;
    }
}
